<<<<<<< HEAD
package com.example.zhangfeng.commondemo.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.example.zhangfeng.commondemo.R;
import com.example.zhangfeng.commondemo.common.ToastUtils;
import com.example.zhangfeng.commondemo.fragment.HomeFragment;
import com.example.zhangfeng.commondemo.fragment.MyFragment;
import com.example.zhangfeng.commondemo.fragment.OtherFragment;

public class MainActivity extends BaseActivity {

    private Fragment[] fragments;
    private int index;
    private Button[] mTabs;

    // 当前fragment的index
    private int currentTabIndex;

    @Override
    public void setWantShowContentView() {
        setContentView(R.layout.activity_main);
        setToolbarTitle("MainActivity");

        initViews();

        initFragment();
    }

    private void initViews() {
        mTabs = new Button[3];
        mTabs[0] = (Button) findViewById(R.id.btn_main_home);
        mTabs[1] = (Button) findViewById(R.id.btn_main_mall);
        mTabs[2] = (Button) findViewById(R.id.btn_main_chat);
        // 把第一个tab设为选中状态
        mTabs[0].setSelected(true);
    }

    private void initFragment() {
        HomeFragment homeFragment = new HomeFragment();
        OtherFragment otherFragment = new OtherFragment();
        MyFragment myFragment = new MyFragment();

        fragments = new Fragment[]{homeFragment, otherFragment, myFragment};
        // 添加显示第一个fragment
        getSupportFragmentManager().beginTransaction().add(R.id.ly_content, homeFragment).commit();
    }

    public void onTabClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_main_home:
                index = 0;
                break;
            case R.id.btn_main_mall:
                index = 1;
                break;
            case R.id.btn_main_chat:
                index = 2;
                break;
        }
        if (currentTabIndex != index) {
            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
            trx.hide(fragments[currentTabIndex]);
            if (!fragments[index].isAdded()) {
                trx.add(R.id.ly_content, fragments[index]);
            }
            trx.show(fragments[index]).commit();
        }
        mTabs[currentTabIndex].setSelected(false);
        // 把当前tab设为选中状态
        mTabs[index].setSelected(true);
        currentTabIndex = index;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (R.id.action_settings == item.getItemId()) {
            ToastUtils.showMessage(this, "click");
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
=======
package com.example.zhangfeng.commondemo.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.example.zhangfeng.commondemo.R;
import com.example.zhangfeng.commondemo.common.ToastUtils;
import com.example.zhangfeng.commondemo.fragment.HomeFragment;
import com.example.zhangfeng.commondemo.fragment.MyFragment;
import com.example.zhangfeng.commondemo.fragment.OtherFragment;

public class MainActivity extends BaseActivity {

    private Fragment[] fragments;
    private int index;
    private Button[] mTabs;

    // 当前fragment的index
    private int currentTabIndex;

    @Override
    public void setWantShowContentView() {
        setContentView(R.layout.activity_main);
        setToolbarTitle("MainActivity");

        initViews();

        initFragment();
    }

    private void initViews() {
        mTabs = new Button[3];
        mTabs[0] = (Button) findViewById(R.id.btn_main_home);
        mTabs[1] = (Button) findViewById(R.id.btn_main_mall);
        mTabs[2] = (Button) findViewById(R.id.btn_main_chat);
        // 把第一个tab设为选中状态
        mTabs[0].setSelected(true);
    }

    private void initFragment() {
        HomeFragment homeFragment = new HomeFragment();
        OtherFragment otherFragment = new OtherFragment();
        MyFragment myFragment = new MyFragment();

        fragments = new Fragment[]{homeFragment, otherFragment, myFragment};
        // 添加显示第一个fragment
        getSupportFragmentManager().beginTransaction().add(R.id.ly_content, homeFragment).commit();
    }

    public void onTabClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_main_home:
                index = 0;
                break;
            case R.id.btn_main_mall:
                index = 1;
                break;
            case R.id.btn_main_chat:
                index = 2;
                break;
        }
        if (currentTabIndex != index) {
            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
            trx.hide(fragments[currentTabIndex]);
            if (!fragments[index].isAdded()) {
                trx.add(R.id.ly_content, fragments[index]);
            }
            trx.show(fragments[index]).commit();
        }
        mTabs[currentTabIndex].setSelected(false);
        // 把当前tab设为选中状态
        mTabs[index].setSelected(true);
        currentTabIndex = index;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (R.id.action_settings == item.getItemId()) {
            ToastUtils.showMessage(this, "click");
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
>>>>>>> 12b9a08f9380bce72b8c6e055d4084faa0570803
